package com.ryder.ryderschool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ryder.ryderschool.entity.Project;
import com.ryder.ryderschool.entity.User;
import com.ryder.ryderschool.mapper.ProjectMapper;
import com.ryder.ryderschool.service.ProjectService;
import com.ryder.ryderschool.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
@RequiredArgsConstructor
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {

    private final UserService userService;

    @Override
    @Transactional
    public void createProject(Project project) {
        User currentUser = userService.getCurrentUser();
        project.setLeaderId(currentUser.getId());
        project.setDeptId(currentUser.getDeptId());
        project.setStatus("DRAFT");
        save(project);
    }

    @Override
    @Transactional
    public void updateProject(Project project) {
        Project existingProject = getById(project.getId());
        if (existingProject == null) {
            throw new RuntimeException("项目不存在");
        }
        User currentUser = userService.getCurrentUser();
        if (!currentUser.getId().equals(existingProject.getLeaderId())) {
            throw new RuntimeException("无权修改此项目");
        }
        updateById(project);
    }

    @Override
    @Transactional
    public void deleteProject(Integer id) {
        Project project = getById(id);
        if (project == null) {
            throw new RuntimeException("项目不存在");
        }
        User currentUser = userService.getCurrentUser();
        if (!currentUser.getId().equals(project.getLeaderId())) {
            throw new RuntimeException("无权删除此项目");
        }
        removeById(id);
    }

    @Override
    public Project getProject(Integer id) {
        return getById(id);
    }

    @Override
    public Page<Project> listProjects(Integer pageNum, Integer pageSize, String keyword) {
        User currentUser = userService.getCurrentUser();
        LambdaQueryWrapper<Project> wrapper = new LambdaQueryWrapper<>();
        
        // 根据用户角色设置查询条件
        switch (currentUser.getRole()) {
            case "TEACHER":
                wrapper.eq(Project::getLeaderId, currentUser.getId());
                break;
            case "DEPT_ADMIN":
                wrapper.eq(Project::getDeptId, currentUser.getDeptId());
                break;
            case "RESEARCH_ADMIN":
                // 科研处管理员可以查看所有项目
                break;
            default:
                throw new RuntimeException("未知的用户角色");
        }
        
        // 添加关键词搜索
        if (StringUtils.hasText(keyword)) {
            wrapper.like(Project::getTitle, keyword)
                    .or()
                    .like(Project::getType, keyword);
        }
        
        return page(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    @Transactional
    public void submitForReview(Integer id) {
        Project project = getById(id);
        if (project == null) {
            throw new RuntimeException("项目不存在");
        }
        User currentUser = userService.getCurrentUser();
        if (!currentUser.getId().equals(project.getLeaderId())) {
            throw new RuntimeException("无权提交此项目");
        }
        project.setStatus("PENDING_FIRST");
        updateById(project);
    }

    @Override
    @Transactional
    public void firstReview(Integer id, boolean approved, String comment) {
        Project project = getById(id);
        if (project == null) {
            throw new RuntimeException("项目不存在");
        }
        User currentUser = userService.getCurrentUser();
        if (!"DEPT_ADMIN".equals(currentUser.getRole())) {
            throw new RuntimeException("只有院系管理员可以进行初审");
        }
        if (!currentUser.getDeptId().equals(project.getDeptId())) {
            throw new RuntimeException("无权审核其他院系的项目");
        }
        
        project.setStatus(approved ? "PENDING_FINAL" : "REJECTED");
        updateById(project);
        
        // TODO: 记录审核日志
    }

    @Override
    @Transactional
    public void finalReview(Integer id, boolean approved, String comment) {
        Project project = getById(id);
        if (project == null) {
            throw new RuntimeException("项目不存在");
        }
        User currentUser = userService.getCurrentUser();
        if (!"RESEARCH_ADMIN".equals(currentUser.getRole())) {
            throw new RuntimeException("只有科研处管理员可以进行终审");
        }
        
        project.setStatus(approved ? "APPROVED" : "REJECTED");
        updateById(project);
        
        // TODO: 记录审核日志
    }
} 